`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Module: AUDIO_TUNE                                                           //
// Author: Zhiyuan Lin                                                          //
// Date: 3/30/2014                                                              //
// Description: This module describe the tune No.2.                             //
// Detail: Currently it is "Bella ciao", an Italian folk                        //
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// UPDATE: Currently 55s of playback  4/28/2014                                 //
//////////////////////////////////////////////////////////////////////////////////
module AUDIO_TUNE2(Speaker, CLK_div, Tone, Restart, Play_END);
	 output Speaker;
	 output reg [5:0] Tone;
	 output Play_END;
	 
	 input  CLK_div, Restart;
	 reg  [23:0] Counter_4Hz, Counter_6MHz;
	 reg  [13:0] Count, Origin;
	 reg  CLK_6MHz;
    reg  [11:0]  Playback_Length;
    reg Speaker_TEMP;
	 
	 reg Play_END = 0;
	 wire Restart;
	 reg CLK_4Hz;
	 
	 
assign Speaker = (Origin != 0) ? Speaker_TEMP : 1'b1;	 
	 
always @(posedge CLK_div) begin   
  if(Counter_6MHz == 4) begin
    Counter_6MHz = 0;
    CLK_6MHz = ~CLK_6MHz;
  end
  else begin
    Counter_6MHz = Counter_6MHz + 24'b1;
  end
end

always @(posedge CLK_div) begin       
  if(Counter_4Hz == 3125000) begin	
    Counter_4Hz = 0;
    CLK_4Hz = ~CLK_4Hz;
  end
  else begin
    Counter_4Hz = Counter_4Hz + 24'b1;
  end
end

always @(posedge CLK_6MHz) begin
  if(Count == 16383) begin //16383 = 11111111111111
    Count = Origin;
    Speaker_TEMP = ~Speaker_TEMP;
  end
  else begin
    Count = Count + 14'b1;
  end
end

always @(posedge CLK_4Hz) begin
  case(Tone)
	 'd0 : Origin = 'd0;
    'd1 : Origin = 'd4916; //low1 (middle C)
	 'd2 : Origin = 'd5560; //low1#
    'd3 : Origin = 'd6168; //low2
	 'd4 : Origin = 'd6740; //low2#
    'd5 : Origin = 'd7281; //low3
    'd6 : Origin = 'd7791; //low4
	 'd7 : Origin = 'd8275; //low4#
    'd8 : Origin = 'd8730; //low5
	 'd9 : Origin = 'd9159; //low5#
    'd10 : Origin = 'd9565; //low6
	 'd11 : Origin = 'd9948; //low6#
    'd12 : Origin = 'd10310; //low7
    'd13 : Origin = 'd10647; //middle1
	 'd14 : Origin = 'd10972; //middle1#
    'd15 : Origin = 'd11272; //middle2
	 'd16 : Origin = 'd11562; //middle2#
    'd17 : Origin = 'd11831; //middle3
    'd18 : Origin = 'd12087; //middle4
	 'd19 : Origin = 'd12329; //middle4#
    'd20 : Origin = 'd12556; //middle5
	 'd21 : Origin = 'd12771; //middle5#
    'd22 : Origin = 'd12974; //middle6
	 'd23 : Origin = 'd13165; //middle6# 
    'd24 : Origin = 'd13346; //middle7
    'd25 : Origin = 'd13516; //high1
	 'd26 : Origin = 'd13677; //high1#	 
    'd27 : Origin = 'd13829; //high2
	 'd28 : Origin = 'd13972; //high2#
    'd29 : Origin = 'd14108; //high3
    'd30 : Origin = 'd14235; //high4
	 'd31 : Origin = 'd14356; //high4#
    'd32 : Origin = 'd14470; //high5
	 'd33 : Origin = 'd14577; //high5#
    'd34 : Origin = 'd14678; //high6
	 'd35 : Origin = 'd14774; //high6#
    'd36 : Origin = 'd14864; //high7
	 'd37 : Origin = 'd14949;
	 'd38 : Origin = 'd15030;
	 'd39 : Origin = 'd15106;
	 'd40 : Origin = 'd15177;
	 'd41 : Origin = 'd15245;
	 'd42 : Origin = 'd15308;
	 'd43 : Origin = 'd15369;
	 'd44 : Origin = 'd15426;
	 'd45 : Origin = 'd15479;
	 'd46 : Origin = 'd15530;
	 'd47 : Origin = 'd15578;
	 'd48 : Origin = 'd15623;
    default : Origin = 'd0;
  endcase                       
end

/*always @(posedge CLK_4Hz)
  begin
	 if (Play_END == 0) 
		begin
		  if(Playback_Length < 226)
		    begin
			   Playback_Length = Playback_Length + 12'b1;
			 end
		  else if (Playback_Length == 226) 
		    begin	
			   Play_END = 1;			
			 end
    end
	 else 
	   begin
		  if (Restart == 1)
			 begin
			   Play_END = 0;
			   Playback_Length = 0;
			 end
		  else
		  ;
	  end
end*/


always @(posedge CLK_4Hz)
  begin
    if (Restart == 0)
	   begin
        if(Playback_Length < 238)
		    begin
			   Playback_Length = Playback_Length + 12'b1;
			 end
		  else if (Playback_Length == 238) 
		    begin	
			   Play_END = 1;			
			 end
    end
	 else
	   begin
	     Play_END = 0;
		  Playback_Length = 0;
		end

end
	
	
always @(posedge CLK_4Hz) begin
  case(Playback_Length)
	  0: Tone = 15; // middle2
	  1: Tone = 17; //middle3
	  2: Tone = 18; //middle4, 5clock
	  3: Tone = 18;
	  4: Tone = 18;
	  5: Tone = 18;
	  6: Tone = 18;
	  7: Tone = 22; //middle6
	  8: Tone = 20; //middle5
	  9: Tone = 18; //middle4
	  10: Tone = 22; //middle6
	  11: Tone = 17; //middle3, 5clock
	  12: Tone = 17;
	  13: Tone = 17;
	  14: Tone = 17;
	  15: Tone = 17;
	  16: Tone = 15; //middle2
	  17: Tone = 13; //middle1
	  18: Tone = 12; //low7, 2clock
	  19: Tone = 12;
	  20: Tone = 17; //middle3, 2clock
	  21: Tone = 17;
	  22: Tone = 12; //low7, 2clock
	  23: Tone = 12;
	  24: Tone = 13; //middle1, 2clock
	  25: Tone = 13;
	  26: Tone = 10; //low6, 5clock
	  27: Tone = 10;
	  28: Tone = 10;
	  29: Tone = 10;
	  30: Tone = 0;
	  31: Tone = 0; //mute for 0.5s
	  32: Tone = 5; //low3
	  33: Tone = 10; //low6
	  34: Tone = 12; //low7
	  35: Tone = 13; //middle1
	  36: Tone = 10; //low6, 4clock
	  37: Tone = 10;
	  38: Tone = 10;
	  39: Tone = 10;
	  40: Tone = 5; //low3
	  41: Tone = 10; //low6
	  42: Tone = 12; //low7
	  43: Tone = 13; //middle1
	  44: Tone = 10; //low6, 4clock
	  45: Tone = 10;
	  46: Tone = 10;
	  47: Tone = 10;
	  48: Tone = 5; //low3
	  49: Tone = 10; //low6
	  50: Tone = 12; //low7
	  51: Tone = 13; //middle1, 2clock
	  52: Tone = 13;
	  53: Tone = 12; //low7
	  54: Tone = 10; //low6
	  55: Tone = 13; //middle1, 2clock
	  56: Tone = 13;
	  57: Tone = 12; //low7
	  58: Tone = 10; //low6
	  59: Tone = 17; //middle3
	  60: Tone = 0;
	  61: Tone = 17; //middle3
	  62: Tone = 0;
	  63: Tone = 17; //middle3
	  64: Tone = 0; 
	    
	  65: Tone = 15; // middle2
	  66: Tone = 17; //middle3
	  67: Tone = 18; //middle4, 5clock
	  68: Tone = 18;
	  69: Tone = 18;
	  70: Tone = 18;
	  71: Tone = 18;
	  72: Tone = 22; //middle6
	  73: Tone = 20; //middle5
	  74: Tone = 18; //middle4
	  75: Tone = 22; //middle6
	  76: Tone = 17; //middle3, 5clock
	  77: Tone = 17;
	  78: Tone = 17;
	  79: Tone = 17;
	  80: Tone = 17;
	  81: Tone = 15; //middle2
	  82: Tone = 13; //middle1
	  83: Tone = 12; //low7, 2clock
	  84: Tone = 12;
	  85: Tone = 17; //middle3, 2clock
	  86: Tone = 17;
	  87: Tone = 12; //low7, 2clock
	  88: Tone = 12;
	  89: Tone = 13; //middle1, 2clock
	  90: Tone = 13;
	  91: Tone = 10; //low6, 5clock
	  92: Tone = 10;
	  93: Tone = 10;
	  94: Tone = 10;
	  95: Tone = 0;
	  96: Tone = 0; //mute for 0.5s
	  97: Tone = 5; //low3
	  98: Tone = 10; //low6
	  99: Tone = 12; //low7
	  100: Tone = 13; //middle1
	  101: Tone = 10; //low6, 4clock
	  102: Tone = 10;
	  103: Tone = 10;
	  104: Tone = 10;
	  105: Tone = 5; //low3
	  106: Tone = 10; //low6
	  107: Tone = 12; //low7
	  108: Tone = 13; //middle1
	  109: Tone = 10; //low6, 4clock
	  110: Tone = 10;
	  111: Tone = 10;
	  112: Tone = 10;
	  113: Tone = 5; //low3
	  114: Tone = 10; //low6
	  115: Tone = 12; //low7
	  116: Tone = 13; //middle1, 2clock
	  117: Tone = 13;
	  118: Tone = 12; //low7
	  119: Tone = 10; //low6
	  120: Tone = 13; //middle1, 2clock
	  121: Tone = 13;
	  122: Tone = 12; //low7
	  123: Tone = 10; //low6
	  124: Tone = 17; //middle3
	  125: Tone = 0;
	  126: Tone = 17; //middle3
	  127: Tone = 0;
	  128: Tone = 17; //middle3
	  129: Tone = 0; 
	    
	  130: Tone = 15; // middle2
	  131: Tone = 17; //middle3
	  132: Tone = 18; //middle4, 5clock
	  133: Tone = 18;
	  134: Tone = 18;
	  135: Tone = 18;
	  136: Tone = 18;
	  137: Tone = 22; //middle6
	  138: Tone = 20; //middle5
	  139: Tone = 18; //middle4
	  140: Tone = 22; //middle6
	  141: Tone = 17; //middle3, 5clock
	  142: Tone = 17;
	  143: Tone = 17;
	  144: Tone = 17;
	  145: Tone = 17;
	  146: Tone = 15; //middle2
	  147: Tone = 13; //middle1
	  148: Tone = 12; //low7, 2clock
	  149: Tone = 12;
	  150: Tone = 17; //middle3, 2clock
	  151: Tone = 17;
	  152: Tone = 12; //low7, 2clock
	  153: Tone = 12;
	  154: Tone = 13; //middle1, 2clock
	  155: Tone = 13;
	  156: Tone = 10; //low6, 5clock
	  157: Tone = 10;
	  158: Tone = 10;
	  159: Tone = 10;
	  160: Tone = 0;
	  161: Tone = 0; //mute for 0.5s
	  162: Tone = 5; //low3
	  163: Tone = 10; //low6
	  164: Tone = 12; //low7
	  165: Tone = 13; //middle1
	  166: Tone = 10; //low6, 4clock
	  167: Tone = 10;
	  168: Tone = 10;
	  169: Tone = 10;
	  170: Tone = 5; //low3
	  171: Tone = 10; //low6
	  172: Tone = 12; //low7
	  173: Tone = 13; //middle1
	  174: Tone = 10; //low6, 4clock
	  175: Tone = 10;
	  176: Tone = 10;
	  177: Tone = 10;
	  178: Tone = 5; //low3
	  179: Tone = 10; //low6
	  180: Tone = 12; //low7
	  181: Tone = 13; //middle1, 2clock
	  182: Tone = 13;
	  183: Tone = 12; //low7
	  184: Tone = 10; //low6
	  185: Tone = 13; //middle1, 2clock
	  186: Tone = 13;
	  187: Tone = 12; //low7
	  188: Tone = 10; //low6
	  189: Tone = 17; //middle3
	  190: Tone = 0;
	  191: Tone = 17; //middle3
	  192: Tone = 0;
	  193: Tone = 17; //middle3
	  194: Tone = 0; 
	  
	  195: Tone = 15; // middle2
	  196: Tone = 17; //middle3
	  197: Tone = 18; //middle4, 5clock
	  198: Tone = 18;
	  199: Tone = 18;
	  200: Tone = 18;
	  201: Tone = 18;
	  202: Tone = 22; //middle6
	  203: Tone = 20; //middle5
	  204: Tone = 18; //middle4
	  205: Tone = 22; //middle6
	  206: Tone = 17; //middle3, 5clock
	  207: Tone = 17;
	  208: Tone = 17;
	  209: Tone = 17;
	  210: Tone = 17;
	  211: Tone = 15; //middle2
	  212: Tone = 13; //middle1
	  213: Tone = 12; //low7, 2clock
	  214: Tone = 12;
	  215: Tone = 17; //middle3, 2clock
	  216: Tone = 17;
	  217: Tone = 19; 
	  218: Tone = 19;
	  219: Tone = 21; 
	  220: Tone = 21;
	  221: Tone = 22; 
	  222: Tone = 0;
	  223: Tone = 17;
	  224: Tone = 0;
	  225: Tone = 22;
	  226: Tone = 0;
     227: Tone = 0;
     228: Tone = 0;
     229: Tone = 0;
     230: Tone = 0;
     231: Tone = 0;
     232: Tone = 0;
     233: Tone = 0;
     234: Tone = 0;
     235: Tone = 0;
     236: Tone = 0;
     238: Tone = 0;	  
	  default: Tone = 0;
  endcase
end
		
endmodule